package edu.arhs.team1100.gamename.handlers;

import edu.arhs.team1100.gamename.objects.Event;
import edu.arhs.team1100.gamename.objects.EventMatch;
import edu.arhs.team1100.gamename.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Eddie
 */
public class EventMatchHandler {

    public static boolean addEventMatch(EventMatch eventMatch) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.save(eventMatch);
        try {
            session.getTransaction().commit();
        } catch (ConstraintViolationException ex) {
            session.getTransaction().rollback();
            return false;
        }

        return true;
    }

    public static void updateEventMatch(EventMatch eventMatch) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(eventMatch);
        session.getTransaction().commit();
    }

    public static List getEventMatches() {
        
        return new ArrayList();
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        session.beginTransaction();
//        List eventMatches = session.createQuery("from EventMatch").list();
//
//        session.getTransaction().commit();
//
//        return eventMatches;
    }
    
    public static boolean deleteEventMatch(EventMatch eventMatch) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
            session.delete(eventMatch);
            session.getTransaction().commit();
        } catch (ConstraintViolationException ex) {
            session.getTransaction().rollback();
            return false;
        }

        return true;
    }
    
     public static List getEventMatchesFromTeam(int teamNumber) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List eventMatches = session.createQuery("from EventMatch where teamNumber = :team").setParameter("team", teamNumber).list();
        session.getTransaction().commit();

        return eventMatches;
    }

    public static List<EventMatch> getEventMatchesFromEvent(Event selectedEvent) {
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        session.beginTransaction();
//        List eventMatches = session.createQuery("from EventMatch where eventID = :eventID").setParameter("eventID", selectedEvent.getEventID()).list();
//        session.getTransaction().commit();
//
//        return eventMatches;
        return new ArrayList();
    }
}
